//
//  SetInfoViewController.m
//  HaoHint
//
//  Created by winter on 16/5/26.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "SetInfoViewController.h"
#import "SetInfoModel.h"
#import "AudioTool.h"

@interface SetInfoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) SetInfoModel *selectModel;
@property (nonatomic, strong) NSIndexPath *selectIndexPath;
@end

@implementation SetInfoViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)setType:(SetInfoViewType)type
{
    _type = type;
    
    SetInfoModel *defaultModel = [[SetInfoModel alloc] init];
    [self.dataArray addObject:@[defaultModel]];
    
    NSMutableArray *tempData = [NSMutableArray array];
    if (SetInfoViewType_Sound == type) {
        defaultModel.soundName = @"系统默认";
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sounds" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
        for (NSString *soundName in array) {
            SetInfoModel *model = [[SetInfoModel alloc] init];
            model.soundName = soundName;
            [tempData addObject:model];
        }
        [self.dataArray addObject:tempData];
    }
    else {
        defaultModel.remindMinutes = -1;
        NSArray *array = @[@"0", @"1", @"2", @"3", @"5", @"10", @"20", @"30", @"60"];
        for (NSString *remindMinutes in array) {
            SetInfoModel *model = [[SetInfoModel alloc] init];
            model.remindMinutes = [remindMinutes integerValue];
            [tempData addObject:model];
        }
        [self.dataArray addObject:tempData];
    }
    self.selectModel = defaultModel;
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.selectModel = [[SetInfoModel alloc] init];
    self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self setupNavBarItem];
}

- (void)setupNavBarItem
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(navigationBarAction:)];
    leftItem.tag = 100;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(navigationBarAction:)];
    rightItem.tag = 101;
    
    [self.navigationItem setRightBarButtonItem:rightItem];
    [self.navigationItem setLeftBarButtonItem:leftItem];
}

- (void)navigationBarAction:(UIBarButtonItem *)sender
{
    [AudioTool stopAudio];
    
    if (101 == sender.tag) {
        if (self.setInfoBlock) {
            if (self.type == SetInfoViewType_Sound) {
                self.setInfoBlock(self.selectModel.soundName);
            }
            else {
                self.setInfoBlock([NSString stringWithFormat:@"%ld",(long)self.selectModel.remindMinutes]);
            }
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - tableView delagate & dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SetInfoModel *model = self.dataArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.cellTitle;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cancelCell = [tableView cellForRowAtIndexPath:self.selectIndexPath];
    cancelCell.accessoryType = UITableViewCellAccessoryNone;
    self.selectIndexPath = indexPath;
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    self.selectModel = self.dataArray[indexPath.section][indexPath.row];
    if (self.type == SetInfoViewType_Sound) {
        // 播放
        [AudioTool playAudio:self.selectModel.soundName];
    }
}
@end
